package 题目集.图.最小生成树;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;

public class P算法 {
    public static void main(String[] args) {
        Reader sc=new Reader();
        int n = sc.nextInt();
        Node[] nodes=new Node[n+1];
        Graph graph=new Graph();
        for (int i = 1; i <= n; i++) {
            nodes[i]=new Node();
            nodes[i].id=i;
            graph.nodes.put(i,nodes[i]);
        }
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int w = sc.nextInt();
            Node from = graph.nodes.get(a);
            Node to = graph.nodes.get(b);
            Edge fromTo=new Edge(from,to,w);
            Edge toFrom=new Edge(to,from,w);
            from.nexts.add(fromTo);
            to.nexts.add(toFrom);
        }
        p(graph,graph.nodes.get(1));
    }

    public static void p(Graph graph,Node s){
        Set<Node> set=new HashSet<>();
        PriorityQueue<Edge> queue=new PriorityQueue<>((o1, o2) -> o1.w-o2.w);
        int res=0;
        set.add(s);
        queue.addAll(s.nexts);
//        res+=queue.peek().w;
        while (!queue.isEmpty()){
            Edge poll = queue.poll();
            if (set.contains(poll.to)){
                continue;
            }
            set.add(poll.to);
            res+=poll.w;
            queue.addAll(poll.to.nexts);
        }
        if(set.size()==graph.nodes.size()){
            System.out.println(res);
        }else{
            System.out.println("impossible");
        }
    }
    static class Graph{
        Map<Integer,Node> nodes=new HashMap<>();

    }
    static class Node{
        int id;
        List<Edge> nexts=new ArrayList<>();
    }
    static class Edge{
        Node from;
        Node to;
        int w;

        public Edge(Node from, Node to, int w) {
            this.from = from;
            this.to = to;
            this.w = w;
        }
    }
    static class Reader {
        static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public static int nextInt() {
            try {
                in.nextToken();
            } catch (Exception ignored) {}
            return (int) in.nval;
        }
    }
}
